Namespace("sketch.ui");

(function(ui) {
    var fwk = sketch.framework;

    ui.iphone.DisclosureButton = klass(fwk.Container, (function() {
        return {
            __type__:"ui.iphone.DisclosureButton",
            _constructor:function() {
                this._fillResource = fwk.Resources['disclosure_button'];
                this.properties.createProperty("arrowPosition", "Arrow position", "bottom").editorTemplate("#editor-dropdown")
                    .possibleValues({"top": "Top", "bottom": "Bottom"})
                    .useInModel();
                var container = new ui.common.DockPannel(true);
                container.bindToSize(this.sizeProperties());
                container.canSelect(false);
                this.width(160);
                this.height(30);
                this.add(container);
                this.enableGroupLocking(true);
            },
            arrowPosition:function(value){
                return this.properties.arrowPosition(value);
            },
            drawSelf:function(context) {
                var x = this.left(),
                    y = this.top(),
                    w = this.width(),
                    h = this.height();
                var tx = 0.25,
                    th = 15,
                    tw = 15,
                    rx = 6,
                    ry = 6;

                context.save();
                context.fillStyle = this._fillResource.toBrush(context, 0, y - th, 0, y + h + th);
                context.strokeStyle = '#43443E';

                context.beginPath();

                if (this.arrowPosition() ==='top') {
                    context.moveTo(x + rx, y);
                    context.lineTo(x + tx * w - tw, y);
                    context.lineTo(x + tx * w, y - th);
                    context.lineTo(x + tx * w + tw, y);
                    context.lineTo(x + w - rx, y);
                    context.quadraticCurveTo(x + w, y, x + w, y + ry);
                    context.lineTo(x + w, y + h - ry);
                    context.quadraticCurveTo(x + w, y + h, x + w - rx, y + h);
                    context.lineTo(x + rx, y + h);
                    context.quadraticCurveTo(x, y + h, x, y + h - ry);
                    context.lineTo(x, y + ry);
                    context.quadraticCurveTo(x, y, x + rx, y);
                } else {
                    context.moveTo(x + rx, y);
                    context.lineTo(x + w - rx, y);
                    context.quadraticCurveTo(x + w, y, x + w, y + ry);
                    context.lineTo(x + w, y + h - ry);
                    context.quadraticCurveTo(x + w, y + h, x + w - rx, y + h);

                    context.lineTo(x + tx * w + tw, y+h);
                    context.lineTo(x + tx * w, y +h + th);
                    context.lineTo(x + tx * w - tw, y+h);

                    context.lineTo(x + rx, y + h);
                    context.quadraticCurveTo(x, y + h, x, y + h - ry);
                    context.lineTo(x, y + ry);
                    context.quadraticCurveTo(x, y, x + rx, y);
                }

                context.fill();
                context.stroke();
                context.closePath();

                context.restore();
                ui.iphone.DisclosureButton.Super.drawSelf.call(this, context);
            }
        };
    })());
})(sketch.ui);